import {ipcMain} from "electron";
import SqliteDB from './db/index'
import ResultDTO from "../../../src/model/ResultDTO";
import MySqlConnectInfo from "../../../src/model/mysql/MySqlConnectInfo";

ipcMain.handle('sqlite-get-connect-info-list', (event) => {
    return new Promise<any>((resolve, reject) => {
        SqliteDB.all("select * from connect_info order by id asc").then(res => {
            console.log("sqlite-get-connect-info-list", res);
            resolve(JSON.stringify(ResultDTO.success(res)))
        }).catch(err => {
            reject(JSON.stringify(ResultDTO.fail(err)))
        })
    })
});
ipcMain.handle('sqlite-update-connection-info', (event, connectInfoJson: string) => {

    console.log("sqlite-update-connection-info", connectInfoJson)

    let connectInfo: MySqlConnectInfo = JSON.parse(connectInfoJson)
    return new Promise<any>((resolve, reject) => {

        SqliteDB.all(`update connect_info
                      set connectName = ?,
                          host        = ?,
                          port        = ?,
                          user        = ?,
                          password    = ?,
                          type        = ?
                      where id = ? `,
            [connectInfo.connectName,
                connectInfo.host, connectInfo.port,
                connectInfo.user, connectInfo.password,
                connectInfo.type, connectInfo.id])
            .then(res => {
                resolve(JSON.stringify(ResultDTO.success(res)))
            }).catch(err => {
            reject(JSON.stringify(ResultDTO.fail(err)))
        })
    })
});

ipcMain.handle('sqlite-add-connection-info', (event, connectInfoJson: string) => {


    let connectInfo: MySqlConnectInfo = JSON.parse(connectInfoJson)
    return new Promise<any>((resolve, reject) => {

        SqliteDB.run(`insert into connect_info (connectName, host, port, user, password, type)
                      values (?, ?, ?, ?, ?, ?)`,
            [connectInfo.connectName,
                connectInfo.host, connectInfo.port,
                connectInfo.user, connectInfo.password,
                connectInfo.type])
            .then(res => {
                console.log("sqlite-add-connection-info: ", connectInfoJson, res)
                connectInfo.id = res.lastID;
                resolve(JSON.stringify(ResultDTO.success(connectInfo)))
            }).catch(err => {
            reject(JSON.stringify(ResultDTO.fail(err)))
        })
    })
});

ipcMain.handle('sqlite-delete-connection-info', (event, connectInfoJson: string) => {


    let connectInfo: MySqlConnectInfo = JSON.parse(connectInfoJson)
    return new Promise<any>((resolve, reject) => {

        SqliteDB.run(`delete from  connect_info  where id = ?`,
            [connectInfo.id])
            .then(res => {
                console.log("sqlite-delete-connection-info: ", connectInfoJson, res)
                resolve(JSON.stringify(ResultDTO.success("")))
            }).catch(err => {
            reject(JSON.stringify(ResultDTO.fail(err)))
        })
    })
});


